<template>
  <a-form @submit="handleSubmit" :form="form" class="form">
    <a-row class="form-row" :gutter="16">
      <a-col :lg="6" :md="12" :sm="24">
        <template v-if="operateTye === 1">
          <a-form-item label="建设类型">
            <a-select :disabled="disabled" placeholder="请选择建设类型" v-decorator="[ 'biudtype', {rules: [{ required: false, message: '请选择建设类型'}]} ]">
              <a-select-option v-for="item in operateTypeData" :key="item.id" :value="item.id">{{item.label}}</a-select-option>
            </a-select>
          </a-form-item>
        </template>
        <template v-else-if="operateTye === 2">
          <a-form-item label="运营类型">
            <a-select :disabled="disabled" placeholder="请选择运营类型" v-decorator="[ 'biudtype', {rules: [{ required: false, message: '请选择运营类型'}]} ]">
              <a-select-option v-for="item in operateTypeData" :key="item.id" :value="item.id">{{item.label}}</a-select-option>
            </a-select>
          </a-form-item>
        </template>
        <template v-else-if="operateTye === 3">
          <a-form-item label="实施类型">
            <a-select :disabled="disabled" placeholder="请选择实施类型" v-decorator="[ 'biudtype', {rules: [{ required: false, message: '请选择实施类型'}]} ]">
              <a-select-option v-for="item in operateTypeData" :key="item.id" :value="item.id">{{item.label}}</a-select-option>
            </a-select>
          </a-form-item>
        </template>
      </a-col>
      <a-col :lg="6" :md="12" :sm="24">
        <a-form-item label="委托机构名称">
          <a-input
            :disabled="disabled"
            placeholder="请输入委托机构名称"
            v-decorator="[
              'entrorg',
                  {rules: [{ required: false, message: '请输入委托机构名称', whitespace: true}, {validator: validate}]}
            ]" />
        </a-form-item>
      </a-col>
    </a-row>
    <a-form-item v-if="showSubmit">
      <a-button htmlType="submit" >Submit</a-button>
    </a-form-item>
  </a-form>
</template>

<script>
  import {getServsysbuildInfo, getServsysoperInfo, getRegrealnalmpInfo} from '@/api/organization';
export default {
  name: 'ServsysbuildForm',
  props: {
    showSubmit: {
      type: Boolean,
      default: false
    },
    operateTye: {
      type: Number,
    }
  },
  data () {
    return {
      form: this.$form.createForm(this),
      disabled: false,
      operateTypeData:[{id:1,label:'自营'},{id:2, label: '委托'}],
      id: '',
    }
  },
  mounted() {

  },
  methods: {
    getDetail(obj, v) {
      const {type, value} = obj
      if (type === 'view') {
        this.disabled = true
        this.getInfo(JSON.parse(value).gnrlid, v)
      } else if (type === 'edit') {
        this.disabled = false;
        this.getInfo(JSON.parse(value).gnrlid, v)
      } else if (type === 'add') {
        this.disabled = false;
        this.form.resetFields()
        this.id = ''
      }
    },
    getInfo(gnrlid, v){
      let that = this
      if (v === 1) {
        getServsysbuildInfo(gnrlid).then(res => {
          const data = res.data
          that.$nextTick(()=>{
            setTimeout(() =>{
              that.id = data.id
              that.form.setFieldsValue(data)
            },0)
          });
        });
      } else if (v === 2) {
        getServsysoperInfo(gnrlid).then(res => {
          const data = res.data
          that.$nextTick(()=>{
            setTimeout(() =>{
              that.id = data.id
              that.form.setFieldsValue(data)
            },0)
          });
        });
      } else if (v === 3) {
        getRegrealnalmpInfo(gnrlid).then(res => {
          const data = res.data
          that.$nextTick(()=>{
            setTimeout(() =>{
              that.id = data.id
              that.form.setFieldsValue(data)
            },0)
          });
        });
      }
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.$notification['error']({
            message: 'Received values of form:',
            description: values
          })
        }
      })
    },
    validate (rule, value, callback) {
      if (this.form.getFieldValue('biudtype') === 2 && !value) {
        callback(new Error('请输入委托机构名称'))
      }
      callback()
    },
  }
}
</script>

<style scoped>

</style>
